/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.domain.repository;

import org.cshoo.tattoo.datacenter.domain.po.ConfigItem;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IConfigItemRepository extends CrudRepository<ConfigItem, Long> {

    /**
     * 根据configId查询item列表
     *
     * @return item列表
     */
    List<ConfigItem> findByConfigIdOrderByCreatedDate(Long configId);

    /**
     * 根据configId查询item列表
     *
     * @return item列表
     */
    List<ConfigItem> findByConfigId(Long configId);

    /**
     * 根据configId和环境查询item
     *
     * @param configId    config id
     * @param environment 环境
     * @return 配置项
     */
    ConfigItem findByConfigIdAndEnvironment(Long configId, String environment);

    /**
     * 根据configId删除config项
     *
     * @param configId configId
     */
    void deleteByConfigId(Long configId);

    /**
     * 更新config项
     *
     * @param configItem config item
     */
    @Query("update ConfigItem configItem set configItem.value = :#{#configItem.value}, configItem.lastModifiedBy = :#{#configItem.lastModifiedBy}, configItem.lastModifiedDate = :#{#configItem.lastModifiedDate} where configItem.id = :#{#configItem.id}")
    @Transactional
    @Modifying
    void updateConfigItem(ConfigItem configItem);
}
